#encoding=utf-8
'''
Created on 2013-2-20

@author: Administrator
'''
from actionModule.handle import base
from base.dbCommon import getPage
from base.common import execMysql, execPage, retSuccess
from config.db.models import Project, Field
import os


formJson = [
      {'id':'id', 'key':'Hidden', 'name':'主键'},
      {'id':'name', 'key':'Input', 'name':'名称', 'noNull':True},
      {'id':'path', 'key':'Input', 'name':'访问路径', 'noNull':True},
      {'id':'package', 'key':'Input', 'name':'包', 'noNull':True},
      {'id':'owner_pro', 'key':'Select', 'name':'所属项目', 'noNull':True},
      {'id':'remark', 'key':'Input', 'name':'备注', 'noNull':True},
      {'key':'Submit', 'action':'service.create','back':'closeTab'},
      {'key':'ServScript'},
]

defPath='/src/service.properties'
servPath='/src'

class service(base):
    def __init__(self):
        self.formJson=formJson
        
    def query(self):
        qryName = self.getByKey("name")
        proId = self.getByKey("proId")
        page = getPage(self.request)
        sql=[]
        sql.append('select a.*,b.`name` proName,b.id proId from db_service a,db_project b where a.owner_pro=b.id ')
        if qryName:
            sql.append("and a.name like '%"+qryName+"%'")
        if proId:
            sql.append("and b.id="+proId)
             
        return execPage("".join(sql),page)
    
    def getServ(self):
        proId = self.getPost("id")
        sql=[]
        sql.append('select a.* from db_service a,db_project b where a.owner_pro=b.id ')
        sql.append("and b.id="+proId)
        return execMysql("".join(sql))
        
    def servFile(self):
        beans=self.getServ()
        pro=Project.objects.get(id=self.getPost("id"))
        pathName=pro['path']+defPath
        servFile=open(pathName,"wb")
        arr=[]
        
        for item in beans:
            arr.append(item['path']+"="+item['package']+"."+item['name'])
            
        servFile.write("\n".join(arr))
        servFile.close()
        
        return retSuccess("生成文件成功："+pathName)
        
    def createServ(self):
        serv=self.table.objects.get(id=self.getPost("serId"))
        pro=Project.objects.get(id=self.getPost("id"))
        package=pro['path']+servPath+"/"+serv['package'].replace(".","/")
        try:
            os.makedirs(package)
        except Exception:
            pass
        
        pathName=package+"/"+serv['name']+".java"
        servFile=open(pathName,"wb")
        arr=[]
        arr.append("package "+serv['package']+";\n")
        arr.append("public class "+serv['name']+" extends com.yqy.util.Service {\n")
        arr.append("}")
        servFile.write("\n".join(arr))
        servFile.close()
        
        return retSuccess("生成文件成功："+pathName)
        
    def saveField(self):
        item = Field()
        item.setAttr("field", self.json['id'])
        item.setAttr("key","'"+self.json['name']+"'")
        item.save()
        self.json["id"]=item.id
        self.json['name']=item.key
        return self.json
    def queryField(self):
        key=self.json["name"]
        return execMysql("SELECT id,db_field.key as text from db_field where field='"+key+"'")
        
    def delField(self):
        arr=self.json["str_array"]
        for item in arr:
            Field.objects.get(id=item).delete()
        return retSuccess("删除成功")      